#!/bin/bash

# PUT THE SH FILE IN THE FOLDER CONTAINING YOUR VIDEOS AND RUN IT

videos=$(ls -f *avi)  ## list the videos you want to process, you can use other extensions


for i in  ${videos} ; ## start the loop
do 

## get the name of your video  that can be used to name the frames later
file=$(echo $i | sed 's!.*/!!' | rev | cut -c5- | rev  | sed 's!.*/!!')  # replace the 5 of the cut function with the length of the extension +2 if not using avi


## get frame rate
frame=$(ffmpeg -i ${i} 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p") 

##Print some info on your video
print "${file} has ${frame} frames/sec"
    
# will extract one frame every 2 frames and store the result as tiff with 4 digits after
ffmpeg -i ${i} -r ${frame}/2 -pix_fmt rgb24  ${file}_%04d.tiff  # if you want all tracks, change the '2' to '1'

 ## create folder to store your frames
mkdir ${file}_frames  
 #move the tiffs to the proper folder
mv $file*.tiff ${file}_frames/   


### images can be used in multiple tracking softwares-> imaris or imageJ

	done

